<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户管理</title>
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/themes/metro/easyui.css"
	rel="stylesheet" />
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/themes/mobile.css"
	rel="stylesheet" />
<link type="text/css"
	href="${pageContext.request.contextPath}/assets/css/common.css"
	rel="stylesheet" />
<style type="text/css">
.row_add {
	color: #2a72c5;
	cursor: pointer;
}

.row_edit {
	color: #2a72c5;
	cursor: pointer;
}

.main {
	margin-right: 15px;
}

ul {
	list-style: none;
}

.icon-list li a {
	cursor: pointer;
}
</style>
</head>
<body>
	<div class="main">
<%-- 	    <!--导航栏-->
        <div class="location">
          <a href="javascript:history.back(-1);" class="back"><i></i><span>返回上一页</span></a>
          <a href="${pageContext.request.contextPath}/bg/home.do" class="home"><i></i><span>首页</span></a>
          <i class="arrow"></i>
          <span>用户管理</span>
        </div>
        <!--/导航栏--> --%>
		<!--工具栏-->
		<div id="floatHead" class="toolbar-wrap">
			<div class="toolbar">
				<div class="box-wrap">
					<a class="menu-btn"></a>
					<div class="l-list">
						<ul class="icon-list">
							<li><a class="add" onclick="add()"><i></i><span>新增</span></a></li>
						</ul>
					</div>
					<div class="r-list">
						<input id="search" class="keyword" /> <a class="btn-search" onclick="searchUser()">查询</a>
					</div>
				</div>
			</div>
		</div>
		<!--/工具栏-->

		<table id="tt"></table>
	</div>

	<div id="dd" style="width:500px;height:400px;">
		<table id="tt1"></table>
		<div style="position:absolute;bottom:20px;padding-left:330px;">
		<button class="btn" onclick="savePermission()">提交保存</button>
		<button class="btn yellow" onclick="cancel()">取消</button>
		</div>
	</div>

    <input type="hidden" id="userId" />
	<script
		src="${pageContext.request.contextPath}/assets/js/jquery-1.11.2.min.js"
		charset="utf-8" type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/jquery.easyui.min.js"
		charset="utf-8" type="text/javascript"></script>
	<script
		src="${pageContext.request.contextPath}/assets/plugins/jquery-easyui-1.4.2/locale/easyui-lang-zh_CN.js"
		charset="utf-8" type="text/javascript"></script>
	<script src="${pageContext.request.contextPath}/assets/js/common.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
	var contextPath = '<%=request.getContextPath()%>';
		$(function() {
			getTableList();
			//$('#content').css('height',$('body').height()-300+'px');
			$(window).resize(function() {
				//延迟执行,防止多次触发
				setTimeout(function() {
					$('#tt').datagrid('resize');
				}, 100);
			});
            $('#dd').dialog({
                closed: true,
                title: '权限管理'
            });
		});
		/*获取数据列表*/
		function getTableList() {
			var search=$('#search').val();
			$('#tt').datagrid({
		        //height: $('body').height() - 270,
		        nowrap: true,
		        striped: true,
		        collapsible: false,
		        remoteSort: false,
		        selectOnCheck: false,
		        checkOnSelect: false,
		        singleSelect: true,
		        fitColumns: true,
		        url: contextPath + "/bg/U102.do?search="+encodeURI(search)+"&rnd=" +Math.random(),
		        columns: [[
		            {
		                field: 'CODE',
		                title: '编码',
		                align: 'center',
		                width: 100
									},
		            {
		                field: 'NAME',
		                title: '姓名',
		                align: 'center',
		                width: 100,
									},
		            {
		                field: 'PHONE_NUMBER',
		                title: '手机号',
		                align: 'center',
		                width: 100
									},
		            {
		                field: 'AGE',
		                title: '年龄',
		                align: 'center',
		                width: 100
									},
		            {
		                field: 'LAST_LOGIN_DATE',
		                title: '上次登录时间',
		                align: 'center',
		                width: 100
									},
		            {
		                field: 'JLZT',
		                title: '状态',
		                align: 'center',
		                width: 100,
		                formatter: function (value, row,
		                    index) {
		                    if (value == "1") {
		                        return '启用';
		                    } else {
		                        return '停用';
		                    }
		                }
									},									
		            {
		                field: 'OTHER1',
		                title: '权限管理',
		                align: 'center',
		                width: 100,
		                formatter: function (id, row, index) {
		                    return '<button type="button" onclick="setPermission(\'' +
	                        row.CODE +
	                        '\')" class="btn">权限</button>';
		                }
									},
		            {
		                field: 'OTHER2',
		                title: '用户中心',
		                align: 'center',
		                width: 100,
		                formatter: function (id, row, index) {
		                    return '<button type="button" onclick="show(\'' +
		                        row.CODE +
		                        '\')" class="btn">查看</button>';
		                }
									},
					{
						field: 'OTHER3',
						title: '用户删除',
						align: 'center',
						width: 100,
						formatter: function (id, row, index) {
						     return '<button type="button" onclick="del(\'' +
						           row.CODE +
						           '\')" class="btn">删除</button>';
						 }
								}]],
		        pagination: true,
		        pageSize: 10,
		        pageList: [10, 15, 30, 40]
		    });
		}
		/*新增*/
		function add() {
			location.href = contextPath + "/bg/user_edit.do?action=add&rnd="
					+ Math.random();
		}
		/*查看*/
		function show(userId) {
			location.href = contextPath
					+ "/bg/user_edit.do?action=edit&userId=" + userId + "&rnd="
					+ Math.random();
		}
		/*权限设置*/
		function setPermission(userId){
			$('#userId').val(userId);
			$('#tt1').treegrid({
				height: 280,
	            striped: true,
	            lines: true,
	            animate: true,
	            nowrap: true,
	            collapsible: true,
	            url: contextPath + "/bg/U105.do?userId="+userId+"&rnd="+Math.random(),
	            idField: 'id',
	            treeField: 'name',
	            fitColumns: true,
	            columns: [[{
	                           field: 'name',
	                           title: '菜单名称',
	                           width: 200,
	                           formatter:function(value,row,index){
                                   return "<input type='checkbox' name=\"pCheck\" data-id=\""+row.id+"\" onclick=check('" + row.id + "')  id='check_" + row.id + "' " + (row.checked=='1' ? 'checked' : '') + "/>" + row.name;
	                           }
	                       },
	                       {
	                           field: 'id',
	                           title: '菜单编码',
	                           width: 100
	                       }
	            ]]
	        });
			$('#dd').dialog('open');
		}
		
		
        function check(checkid) {
            var s = '#check_' + checkid;
            /*选子节点*/
            var nodes = $("#tt1").treegrid("getChildren", checkid);          
            for (i = 0; i < nodes.length; i++) {           	
                $(('#check_' + nodes[i].id))[0].checked = $(s)[0].checked;
            }
            //选上级节点
            if (!$(s)[0].checked) {
/*                 var parent = $("#tt1").treegrid("getParent", checkid);
                while (parent) {
                    var sons = parent.children;
                    var flag = false;
                    for (j = 0; j < sons.length; j++) {
                        if ($(('#check_' + sons[j].id))[0].checked) {
                            brother_checked = true;
                            break;
                        }
                    }
                    if (!flag) $(('#check_' + parent.id))[0].checked = false;          
                    parent = $("#tt1").treegrid("getParent", parent.id);
                } */
            } else {
                var parent = $("#tt1").treegrid("getParent", checkid);
                while(parent){
/*                 	var flag = true;
                    var sons = parent.children;
                    for (j = 0; j < sons.length; j++) {
                        if (!$(('#check_' + sons[j].id))[0].checked) {
                            flag = false;
                            break;
                        }
                    }
                    if (flag) $(('#check_' + parent.id))[0].checked = true; */
                    $(('#check_' + parent.id))[0].checked = true;
                    parent = $("#tt1").treegrid("getParent", parent.id);                                        
                }
            }  
        }
        function cancel(){
        	$('#dd').dialog('close');
        }
        function savePermission(){
        	var checkedValue='';
        	$('input[name="pCheck"]:checked').each(function(i){
        		checkedValue+=$(this).attr('data-id')+',';
        	})
        	if(checkedValue.length>0) checkedValue=checkedValue.substring(0,checkedValue.length-1);
        	
        	var param={};
        	param.rnd=Math.random();
        	param.checkedValue=checkedValue;
        	param.userId=$('#userId').val();
			$.ajax({
				url : contextPath + "/bg/U106.do",
				data : param,
				dataType : 'json',
				success : function(data, textStatus, jqXHR) {	
					$.messager.show({title:'提示',msg:'权限修改成功！',showType:'slide'});
					$('#dd').dialog('close');
				},
				error : function(jqXHR, textStatus, errorThrown) {
					$.messager.show({title:'提示',msg:'权限修改失败！',showType:'slide'});
				}
			});
        }
        function searchUser(){
        	getTableList();
        }
        //删除用户
        function del(userId){
        	var param={};
        	param.rnd=Math.random();
        	param.userId=userId;
		    $.messager.confirm('确认', '确认删除用户及其权限?', function(r){
		    	if (r){
					$.ajax({
						url : contextPath + "/bg/U108.do",
						data : param,
						dataType : 'json',
						success : function(data, textStatus, jqXHR) {	
							if(data.status=='true'){
								$.messager.show({title:'提示',msg:'用户删除成功！',showType:'slide'});
								getTableList();
							}else{
								$.messager.show({title:'提示',msg:'用户删除失败！',showType:'slide'});
							}
						},
						error : function(jqXHR, textStatus, errorThrown) {
							$.messager.show({title:'提示',msg:'用户删除失败！',showType:'slide'});
						}
					});
		    	}
		    });
        }
	</script>
</body>
</html>